package com.blrs.service.file.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.blrs.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.PermissionSettingMapper;
import com.blrs.domain.PermissionSetting;
import com.blrs.service.file.IPermissionSettingService;

/**
 * 内部文件权限设置Service业务层处理
 * 
 * @author wangchao
 * @date 2023-12-20
 */
@Service
public class PermissionSettingServiceImpl implements IPermissionSettingService 
{
    @Autowired
    private PermissionSettingMapper permissionSettingMapper;

    /**
     * 查询内部文件权限设置
     * 
     * @param permissionId 内部文件权限设置主键
     * @return 内部文件权限设置
     */
    @Override
    public PermissionSetting selectPermissionSettingByPermissionId(String permissionId)
    {
        return permissionSettingMapper.selectPermissionSettingByPermissionId(permissionId);
    }

    /**
     * 查询内部文件权限设置列表
     * 
     * @param permissionSetting 内部文件权限设置
     * @return 内部文件权限设置
     */
    @Override
    public List<PermissionSetting> selectPermissionSettingList(PermissionSetting permissionSetting)
    {
        return permissionSettingMapper.selectPermissionSettingList(permissionSetting);
    }

    /**
     * 新增内部文件权限设置
     * 
     * @param permissionSetting 内部文件权限设置
     * @return 结果
     */
    @Override
    public int insertPermissionSetting(PermissionSetting permissionSetting)
    {
        PermissionSetting data = new PermissionSetting();
//        if (permissionSetting.getPermissionUserId().equals(SecurityUtils.getUserId())){
//            return 1;
//        }
        data.setPermissionUserId(permissionSetting.getPermissionUserId());
        List<PermissionSetting> list = selectPermissionSettingList(data);
        if(list.size()>0){
            deletePermissionSettingByPermissionId(list.get(0).getPermissionId());
        }
        permissionSetting.setPermissionId(UUID.randomUUID().toString());
        permissionSetting.setCreationId(SecurityUtils.getUserId().toString());
        permissionSetting.setCreationName(SecurityUtils.getUsername());
        permissionSetting.setCreationTime(new Timestamp(new Date().getTime()));
        return permissionSettingMapper.insertPermissionSetting(permissionSetting);
    }

    /**
     * 修改内部文件权限设置
     * 
     * @param permissionSetting 内部文件权限设置
     * @return 结果
     */
    @Override
    public int updatePermissionSetting(PermissionSetting permissionSetting)
    {
        return permissionSettingMapper.updatePermissionSetting(permissionSetting);
    }

    /**
     * 批量删除内部文件权限设置
     * 
     * @param permissionIds 需要删除的内部文件权限设置主键
     * @return 结果
     */
    @Override
    public int deletePermissionSettingByPermissionIds(String[] permissionIds)
    {
        return permissionSettingMapper.deletePermissionSettingByPermissionIds(permissionIds);
    }

    /**
     * 删除内部文件权限设置信息
     * 
     * @param permissionId 内部文件权限设置主键
     * @return 结果
     */
    @Override
    public int deletePermissionSettingByPermissionId(String permissionId)
    {
        return permissionSettingMapper.deletePermissionSettingByPermissionId(permissionId);
    }
}
